package com.android.pingpong;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.view.SurfaceHolder;
import com.android.pingpong.Maze;
import com.android.pingpong.Cell;

public class GameManager extends Thread
{
    private static final int FIELD_WIDTH = 300;
    
    private static final int FIELD_HEIGHT = 250;
   int xsize = 25, ysize = 10;
   static int WIDTH = FIELD_WIDTH;
   static int HEIGHT = FIELD_HEIGHT;
   
    private SurfaceHolder mSurfaceHolder;
    

    private boolean mRunning;
    

    private Paint mPaint;
    private Paint mPaintWall;

    private Rect mField;
	private Cell[][] maze = null;

    public GameManager(SurfaceHolder surfaceHolder, Context context)
    {
        mSurfaceHolder = surfaceHolder;
        mRunning = false;
        
        maze = Maze.backTracker(xsize, ysize);
       
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Style.STROKE);
        
        mPaintWall = new Paint();
        mPaintWall.setColor(Color.RED);
        mPaintWall.setStrokeWidth(3);
        mPaintWall.setStyle(Style.STROKE);
        
        mField = new Rect();
    }
    
	public void renderMaze(Canvas c, Cell[][] maze, int xsize, int ysize) throws Exception {
		
		for (int x = 0; x < xsize; x++) {
			for (int y = 0; y < ysize; y++) {
				drawWalls(c, xsize, ysize, maze[x][y]);		
			}
			System.out.print("one");
		}	
		//solve(g, maze[0][0], xsize, ysize);
	}
    
	public void drawWalls(Canvas c, int xsize, int ysize, Cell cell) {

		//int width = WIDTH/(xsize + 1)+5;
		int height = HEIGHT/(ysize + 1);
		int width =	(int)( (double)height/1.2);
		int x = cell.x*width;
		int y = cell.y*height;
		int wallWidth = width/3;
	    
	    
		Rect r1 = new Rect();
		
		if (cell.left == null) {
			r1.set(x, y, x+wallWidth, y+height );
			c.drawRect(r1, mPaintWall);
		}
		if (cell.right == null) {
			r1.set(width + x, y,width + x + wallWidth, y+ height);
			c.drawRect(r1, mPaintWall);
		}
		if (cell.up == null) {
			r1.set(x, y, x + width, y + wallWidth);
			c.drawRect(r1, mPaintWall);
		}
		if (cell.down == null) {
			r1.set(x, height + y, x + width,height + y+ wallWidth);
			c.drawRect(r1, mPaintWall);
		}
		
	}

    public void setRunning(boolean running)
    {
        mRunning = running;
    }
    
    @Override
    public void run()
    {
        while (mRunning)
        {
            Canvas canvas = null;
            try
            {
                canvas = mSurfaceHolder.lockCanvas(); 
                synchronized (mSurfaceHolder)
                {
                   // canvas.drawRect(mField, mPaint);
                    renderMaze(canvas,maze,xsize,ysize);
                   // drawWalls(canvas, 1, 1, new Cell(0,0));		
                }
            }
            catch (Exception e) { }
            finally
            {
                if (canvas != null)
                {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
    
    public void initPositions(int screenHeight, int screenWidth)
    {
        int left = (screenWidth - FIELD_WIDTH) / 2;
        int top = (screenHeight - FIELD_HEIGHT) / 2;
        
        mField.set(left, top, left + FIELD_WIDTH, top + FIELD_HEIGHT);
    }
}
